package v1

import (
	"github.com/astaxie/beego"
	"pq/models"
)

type SlTestItemController struct{
	TokenCheckController
}
func (sic *SlTestItemController) Post(){
	row := new (models.PqSlTestItem)
	id,_ := sic.GetInt("id",0)
	row.Body = sic.GetString("body")
	row.PqSlTestLibraryId,_ = sic.GetInt("pq_sl_test_library_id")
	row.Analysis = sic.GetString("analysis")
	row.Score,_ = sic.GetInt("score")
	master := models.GetMasterDb()
	if id > 0 {
		row.Id = id
		_,err := master.Update(row)
		if err != nil{
			beego.Error(err)
		}
	}else{
		row.Id = 0
		row.State = 1
		ins, err := master.Insert(row)
		if err != nil{
			beego.Error(err)
		}
		id = int(ins)
	}
	item := new (models.PqSlTestItem)
	item.SaveQuestion(row.PqSlTestLibraryId)
	res := map[string]interface{}{}
	res["id"] = id
	sic.Single(res)
}

func (sic *SlTestItemController) Del(){
	id := sic.Ctx.Input.Param(":id")
	master := models.GetMasterDb()
	slave := models.GetSlaveDb()
	var mi models.PqSlTestItem
	slave.QueryTable("pq_sl_test_item").Filter("id",id).One(&mi)
	mi.State = -1
	if _,err := master.Update(&mi);err != nil{
		sic.Fail("Deleted fail","")
	}
	item := new (models.PqSlTestItem)
	item.SaveQuestion(mi.PqSlTestLibraryId)
	sic.Success("Deleted Successfully")
}